package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;
    /**
     * 新增地址
     */
    @PostMapping
    @ApiOperation(value = "新增地址")
    public Result<String> add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);

        return Result.success();
    }
    /**
     * 查寻当前登录用户的所有地址信息
     */
    @GetMapping("/list")
    @ApiOperation(value = "查寻当前登录用户的所有地址信息")
    public Result<List<AddressBook>> getAllAddressByUserId(){
        List<AddressBook> list= addressBookService.getAllAddress();
        return Result.success(list);
    }
    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    @ApiOperation(value = "查询默认地址")
    public Result<AddressBook> getDefaultAddress(){
        AddressBook defaultAddress = addressBookService.getDefaultAddress();
        return Result.success(defaultAddress);
    }
    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询地址")
    public Result<AddressBook> getAddressById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getAddressById(id);
        return Result.success(addressBook);
    }
    /**
     * 设置为默认地址
     */
    @PutMapping("/default")
    @ApiOperation(value = "设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.updateDefault(addressBook);
        return Result.success();
    }

    /**
     * 删除地址
     */
    @DeleteMapping
    @ApiOperation(value = "根据id删除地址")
    public Result deleteAddress(Long id) {
        addressBookService.deleteById(id);
        return Result.success();
    }
    /**
     * 修改地址
     */
    @PutMapping
    @ApiOperation(value = "修改地址")
    public Result updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

}



















